package com.yanqu.road.server.cmd.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_ACTIVITY_RANK_LIST, desc = "个人活动积分排行榜")
public class ActivityRankListCmd extends AbstractCommand {

    private int activityId;
    private int conditionType;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getRankType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData(GamePlayer player) {
        CommonActivityProto.CommonActivityRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        ActivityInfo openActivityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (null != openActivityInfo) {

            // 商会分组活动走这边
            if (UnionActivityGroupHelper.isGroupActivityType(openActivityInfo.getType())) {
                YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
                builder.setActivityId(openActivityInfo.getActivityId());
                builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
                builder.setIdKey(String.valueOf(conditionType));
                if(builder.getGroupId() == UnionActivityGroupHelper.NO_GROUP_ID && openActivityInfo.getType() == eActivityType.Tomb.getValue()){
                    builder.setGroupId(GameUnionActivityGroupMgr.getViewDataGroupId(activityId, player.getUserId()));
                    player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_LIST, builder, activityId, builder.getGroupId());
                }else {
                    player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_USER_RANK_LIST, builder, activityId);
                }
                return;
            }

            boolean isCrossRank = ActivityHelper.conditionTypeIsCrossRank(conditionType);
            if(isCrossRank){//跨服活动
                /*RankProto.RankListMsg.Builder rankListMsg = CrossDataMgr.getCrossUserRankActivityRankMsg(activityId, conditionType);
                respMsg.setRankList(rankListMsg);
                respMsg.setMyRank(ActivityMgr.getMyCrossRank(player.getUserId(), rankListMsg));*/
                CommonActivityProto.CommonActivityRankListReqMsg.Builder msg = CommonActivityProto.CommonActivityRankListReqMsg.newBuilder();
                msg.setActivityId(activityId);
                msg.setRankType(conditionType);
                player.sendPacket(Protocol.C2_CROSS_GET_USER_ACTIVITY_RANK, msg);
                return;
            }else {
                RankListModel rankListModel = ActivityRankMgr.getActivityRankListModel(activityId, conditionType);
                if (null != rankListModel) {
                    respMsg.setRankList(rankListModel.getRankListMsg());
                    respMsg.setMyRank(rankListModel.unlockGetMyRank(player.getUserId()));
                }
            }
        }
        player.sendPacket(Protocol.U_ACTIVITY_RANK_LIST, respMsg);
    }
}
